// 分组背包问题
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 110, M = 2 * N;

int h[N], e[M], ne[M], w[M], idx;
int n, m;
int dp[N][N]; // dp[i][j] 以i为节点，保留j条树枝的所留下的最大苹果数

void add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

int dfs(int u, int father)
{
    // 物品组
    for (int i = h[u]; i != -1; i = ne[i]) 
    {
        int j = e[i];
        if (j == father) continue;
        int d = dfs(j, u);
        // 体积
        for (int k = m; k; k -- )
            // 决策
            for (int l = 0; l + 1 <= k; l ++ )
                dp[u][k] = max(dp[u][k], dp[u][k - l - 1] + dp[j][l] + w[i]);
    }
}

int main()
{
    memset(h, -1, sizeof h);
    cin >> n >> m;
    for (int i = 1; i < n; i ++ )
    {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c), add(b, a, c);
    }
    dfs(1, -1);
    cout << dp[1][m];
    return 0;
}
